# -*- coding: utf-8 -*-
import random
import re
import time

import requests

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

# from selenium.webdriver.common.keys import Keys
# from selenium.webdriver.common.action_chains import ActionChains

# print(os.path)
#path加上完整的driver名称
#driver版本最新为2.40
#http://icanhazip.com
blackList = [
"61.135.217.7:80",
]
whiteList = [
]

targetkw = r"洁净"
jingpinkw = r"清洁"
zhidingkw = ""

#在元素中找到关键字
def find_KW_in_Elems(driver, elems, kw):
    temp = []
    try:
        if kw == "":
            return random.sample(elems, 1)[0]
        for jp in elems:
            for i in jp.find_elements_by_tag_name('a'):
                if i.get_attribute('href'):
                    temp.append(i.get_attribute('text'))
            if kw in ' '.join(temp):
                return jp

    except Exception as e:
        print("found none", e)
        return None

def turnPage(driver):
    try:
        driver.find_element_by_class_name("next").click()
        time.sleep(random.uniform(3,5))
        driver.switch_to.window(driver.window_handles[-1])
        print("page:%s" %re.findall(r'/pn\d+/', driver.current_url)[0][3:-1])
        driver.switch_to.window(driver.window_handles[-1])
        return driver
    except:
        print("error in turnpage")
        return None

def classify_items(driver):
    jingpin = []
    zhiding = []
    normal = []
    try:
        ac_item_list = driver.find_elements_by_class_name("ac_item")
        # print("ac_item_list",ac_item_list)
        none_list = driver.find_elements_by_class_name("none")
        # print("none",none_list)

        for ac in ac_item_list:
            if ac in none_list:
            #   print("none")
                continue
            try:
                ac.find_element_by_class_name("jingpin")
                jingpin.append(ac)
                # print("jingpin")
                continue
            except:
                # print("not jingpin")
                pass
                
            try:
                ac.find_element_by_class_name("ding")
                zhiding.append(ac)
                # print("zhiding")
                continue
            except:
                normal.append(ac)
                # print("normal")
        print("all：精：%d, 顶：%d, 普通：%d" %(len(jingpin), len(zhiding),len(normal)))
        return (jingpin, zhiding, normal)
    except:
        print("error in classify items")
        return (jingpin, zhiding, normal)

'''
传入元素，打开，过段时间关闭
'''
def flash_page_by_element(driver, elemt, delay = 30):

    try:
        elemt.click()
        print("open",driver.title)
        time.sleep(random.uniform(delay-5, delay+5))
        driver.switch_to.window(driver.window_handles[-1])
        print("close",driver.title)
        driver.close()
        driver.switch_to.window(driver.window_handles[-1])
        return driver
    except Exception as e:
        print(e)
        return None
    pass

def main():
    executable_path = r"C:\Users\Administrator\AppData\Local\Google\Chrome SxS\Application\chromedriver.exe"
    while(True):
        proxy = requests.get(url = "http://127.0.0.1:5010/get").text#"212.8.252.106:1080"#
        if len(proxy)<4:
            break
        elif proxy in blackList: 
            continue
        else:
            print("tring:%s:" %proxy)

        # time.sleep(2)
        
        options = webdriver.ChromeOptions()
        options.add_argument("--proxy-server=%s" % proxy)
        # options.add_argument('--headless')
        # driver = webdriver.Chrome(executable_path=executable_path, chrome_options=options)
        driver = webdriver.Chrome(executable_path=executable_path, chrome_options=options)#)#
        
    #    PROXY = proxy
    #    # Create a copy of desired capabilities object.
    #    desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER.copy()
    #    # Change the proxy properties of that copy.
    #    desired_capabilities['proxy'] = {
    #        "httpProxy":PROXY,
    #        "ftpProxy":PROXY,
    #        "sslProxy":PROXY,
    #        "noProxy":None,
    #        "proxyType":"MANUAL",
    #        "class":"org.openqa.selenium.Proxy",
    #        "autodetect":False
    #    }
    #    driver = webdriver.Remote("http://icanhazip.com", desired_capabilities)
        
        # driver.get(r'http://icanhazip.com')
        driver.get(r'https://www.baidu.com')
        # print(bd) #none
        try:
			# print(driver.title)
            # time.sleep(random.uniform(0,3))
            WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,'kw')))
            driver.find_element_by_id('kw').send_keys(r'洛阳58')
            driver.find_element_by_id('su').click()
            # time.sleep(random.uniform(1,5))
            weblist = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.TAG_NAME,'h3')))
            # weblist = driver.find_elements_by_tag_name('h3')
            # print (weblist)
            print(len(weblist))
            print(weblist[0].text)
        #    if len(weblist) > 0:
        #        weblist[0].click()
        #    else:
        #        continue
            weblist[0].click()
            time.sleep(random.uniform(2,5))
			# 
            print(driver.window_handles)
			# break
            driver.switch_to.window(driver.window_handles[-1])
            print(driver.title)
        #    time.sleep(random.uniform(10,15))#################waite ad
            try:
                fl_ad =  driver.find_element_by_class_name("fl_cont_pic")
                while(fl_ad.is_displayed()):
                    time.sleep(1)
            except:
                time.sleep(random.uniform(12,15))
                pass            
            # panel = WebDriverWait(driver,30).until(EC.visibility_of(driver.find_element(by=By.CLASS_NAME,value='rightSide')))
            # panel = driver.find_element_by_class_name("rightSide")
            # baojie = WebDriverWait(driver,30).until(EC.visibility_of(panel.find_element(by=By.LINK_TEXT, value='保洁')))
            WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div[1]/div[2]/div[2]/div/div[1]/em[3]/a'))).click()
            # WebDriverWait(driver,10).until(EC.visibility_of(panel.find_element(by=By.LINK_TEXT, value='保洁'))).click()
            # panel.find_element_by_link_text("保洁").click()
            driver.switch_to.window(driver.window_handles[-1])
            print(driver.title)
            time.sleep(random.uniform(3,8))
            #找到所有广告列表

            # time.sleep(random.uniform(1,5))
            jplist = []
            zdlist = []
            nmlist = []
            # find target page
            while(1):
                print("searching target")
                jplist,zdlist,nmlist = classify_items(driver)
                targetitem = find_KW_in_Elems(driver, nmlist, targetkw)
                if targetitem:
                    break
                else:
                    driver = turnPage(driver)
            print("found it")
            imgelemt = targetitem.find_element_by_class_name("img")
            driver = flash_page_by_element(driver, imgelemt)

            if len(jplist)>0:
                print(driver.title)
                jpitem = find_KW_in_Elems(driver, jplist, jingpinkw)
                
                if jpitem == None:
                    while(1):
                        driver = turnPage(driver)
                        print("searching target")
                        jplist,zdlist,nmlist = classify_items(driver)
                        jpitem = find_KW_in_Elems(driver, jplist, jingpinkw)
                        if jpitem:
                            break
                        
                imgelemt = jpitem.find_element_by_class_name("img")#("item-desc")
                driver = flash_page_by_element(driver, imgelemt)
                
            if len(zdlist)>0:
                driver.switch_to.window(driver.window_handles[-1])
                zditem = find_KW_in_Elems(driver, zdlist, zhidingkw)
                driver = flash_page_by_element(driver, zditem.find_element_by_class_name("img"))

            print(len(driver.page_source))
            
            time.sleep(random.uniform(5,10))
            driver.quit()
        except Exception as e:
            print(e)
            driver.quit()
			# pass
            # break
	# pass

if __name__ == '__main__':
	main()

"""
Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"71E7FAC340E
3EE2ACA2A365E53644AC4","isDefault":true},"id":1,"name":"","origin":"://"}


PROXY_IP = "<public proxy IP address>"

options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=%s" % PROXY_IP)

driver = webdriver.Chrome(executable_path=".\\driver\\chromedriver.exe",
                          chrome_options=options)
driver.get("<site URL>")
"""
